<?php
namespace App\Controller;

class OrdersController extends AppController{

    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.19
   * ps：获取退货详情
   * */
    public function getTreturnApplyDetail()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData('id');
        $is_sell = $this->request->getData('is_sell');
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(!is_numeric($is_sell)){
            $this->G->error("is_sell_error");
            return null;
        }
        $user_id = $this->Jwt->get('user_id');
        $this->loadModel('UserCompanies');
        $user_companies = $this->UserCompanies->findByUserId($user_id)->first();
        if(empty($user_companies)){
            $this->G->error("company_error");
            return null;
        }
        if($is_sell == 1){
            $where['return_company_id'] = $user_companies['company_id'];
        }else{
            $where['company_id'] = $user_companies['company_id'];
        }
        $this->loadModel('OrderReturnApplies');
        $order_return_applies = $this->OrderReturnApplies->findById($id)->where($where)->first();
        $orders = $this->Orders->findById($order_return_applies['order_id'])->first();
        $this->G->success("Success", ['order'=>$orders, 'order_return_apply'=>$order_return_applies]);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.19
   * ps：获取退货列表
   * */
    public function getTreturnApplyList()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $page = $this->request->getData('page');
        $return_sn = $this->request->getData('return_sn');
        $sn = $this->request->getData('sn');
        $status = $this->request->getData('status');
        $create_time = $this->request->getData('create_time');
        $handle_user_username = $this->request->getData('handle_user_username');
        $handle_time = $this->request->getData('handle_time');
        $is_sell = $this->request->getData('is_sell');
        $where = [];
        $page = empty($page) ? 1:$page;
        if(!empty($sn)){
            $where['order_sn like'] = '%'.$sn.'%';
        }
        if(!empty($return_sn)){
            $where['return_sn like'] = '%'.$return_sn.'%';
        }
        if(!empty($receiver_phone)){
            $where['receiver_phone like'] = '%'.$receiver_phone.'%';
        }
        if(!empty($handle_user_username)){
            $where['handle_user_username like'] = '%'.$handle_user_username.'%';
        }
        if(!empty($create_time)){
            $where['create_time >='] = strtotime($create_time);
        }
        if(!empty($handle_time)){
            $where['handle_time'] = strtotime($handle_time);
        }
        if(is_numeric($status)){
            $where['status'] = $status;
        }
        $user_id = $this->Jwt->get('user_id');
        $this->loadModel('UserCompanies');
        $user_companies = $this->UserCompanies->findByUserId($user_id)->first();
        if(empty($user_companies)){
            $this->G->error("company_error");
            return null;
        }
        if($is_sell == 1){
            $where['return_company_id'] = $user_companies['company_id'];
        }else{
            $where['company_id'] = $user_companies['company_id'];
        }
        $this->loadModel('OrderReturnApplies');
        $order_return_applies = $this->OrderReturnApplies->find()
            ->select(['id','return_sn','create_time','return_company_title','return_amount','status','handle_time'])
            ->where($where)
            ->order(['create_time desc'])
            ->limit(30)
            ->page($page);
        $re = [];
        $count = $order_return_applies->count();
        $re['pageTotal'] = ceil($count/30);
        $order_return_applies = $order_return_applies->toArray();
        $re['list'] = [];
        if(!empty($order_return_applies)){
            foreach ($order_return_applies as $k=>$v){
                $order_return_applies[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $order_return_applies[$k]['handle_time'] = date('Y-m-d H:i:s', $v['handle_time']);
                $re['list'][] = $order_return_applies[$k];
            }
        }
        $this->G->success("Success", $re);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.19
   * ps：获取订单详情
   * */
    public function getInfo()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData('id');
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $orders = $this->Orders->findById($id)->first();
        $this->loadModel('OrderItems');
        $order_items = $this->OrderItems->findByOrderId($id)->toArray();
        $this->loadModel('OrderHistories');
        $order_histories = $this->OrderHistories->findByOrderId($id)->toArray();
        if(!empty($order_histories)){
            $this->loadModel('Users');
            $users = $this->Users->find()
                ->select(['id','username'])
                ->where(['id in'=>array_column($order_histories, 'operate_user_id')])
                ->toArray();
            $user = [];
            if(!empty($users)){
                foreach ($users as $k=>$v){
                    $user[$v['id']] = $v['username'];
                }
            }
            foreach ($order_histories as $k=>$v){
                $order_histories[$k]['operate_user_username'] = empty($user[$v['operate_user_id']]) ? '':$user[$v['operate_user_id']];
            }
        }
        $this->G->success("Success", ['order'=>$orders,'order_items'=>$order_items,'order_history'=>$order_histories]);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.19
   * ps：获取订单列表
   * */
    public function getList()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $sn = $this->request->getData('sn');
        $receiver_name = $this->request->getData('receiver_name');
        $receiver_phone = $this->request->getData('receiver_phone');
        $create_time = $this->request->getData('create_time');
        $status = $this->request->getData('status');
        $page = $this->request->getData('page');
        $where = [];
        $page = empty($page) ? 1:$page;
        if(!empty($sn)){
            $where['sn like'] = '%'.$sn.'%';
        }
        if(!empty($receiver_name)){
            $where['receiver_name like'] = '%'.$receiver_name.'%';
        }
        if(!empty($receiver_phone)){
            $where['receiver_phone like'] = '%'.$receiver_phone.'%';
        }
        if(!empty($create_time)){
            $where['create_time >='] = strtotime($create_time);
        }
        if(is_numeric($status)){
            $where['status'] = $status;
        }
        $orders = $this->Orders->find()
            ->select(['id','sn','create_time','sell_company_title','buy_company_title','total_amount','pay_type','source_type','status'])
            ->where($where)
            ->order(['create_time desc'])
            ->limit(30)
            ->page($page);
        $re = [];
        $count = $orders->count();
        $re['pageTotal'] = ceil($count/30);
        $orders = $orders->toArray();
        $re['list'] = [];
        if(!empty($orders)){
            foreach ($orders as $k=>$v){
                $orders[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $re['list'][] = $orders[$k];
            }
        }
        $this->G->success("Success", $re);
    }

}
?>